import com.dmotiko.bolacua.*
import com.dmotiko.general.*
import com.dmotiko.util.*
import mx.utils.Delegate

class PhotoPopUp
extends BaseClip {
	
	private var mcMiddle:MovieClip;
	private var mcBottom:MovieClip;
	private var mcMask:MovieClip;
	private var mcPic:MovieClip;
	
	private var fadeIn:Function;
	private var fadeOut:Function;
	private var bShowed:Boolean;
		
	private function PhotoPopUp() {
		super();
		//BolacuaSite.getApp().log( "new PhotoPopUp" );
		var root:PhotoPopUp = this;
		BolacuaSite.getApp().setPhotoPopUp(this);
		MovieEffects.fadeIn();
		MovieEffects.fadeOut();
		mcPic = this.createEmptyMovieClip("mcPic", this.getNextHighestDepth());
		mcMiddle.setMask(mcMask);
		mcMiddle.onRelease = function() {
			root.hide();
		}
		picHideEnd();
		if ( BolacuaSite.getApp().getPromos().length > 0) {
			setData( { index: 0, photos: BolacuaSite.getApp().getPromos() } );
		} else {
			BolacuaSite.getApp().addEventListener( BolacuaSite.SECTION_CHANGED, Delegate.create(this, hide));
		}
	}
	
	public function isShowed():Boolean {
		return bShowed;
	}
	
	private function refreshData():Void {
		if (data.photos[ data.index ].getPromo()) {
			data.posY = 270;
		} else {
			data.posY = _ymouse;
		}
		this.show();
	}
	
	public function show() {
		this.bShowed = true;
		this._alpha = 0;
		this._visible = true;
		var nHeight:Number;
		switch(BolacuaSite.getApp().getSection()) {
			case BolacuaSite.HOME:
			nHeight = 658 - 132;
			break;
			
			case BolacuaSite.ALOJAMIENTO:
			nHeight = 2400 - 149;
			break;
			
			case BolacuaSite.RESTAURANT:
			nHeight = 1300 - 149;
			break;
			
			case BolacuaSite.INFRAESTRUCTURA:
			nHeight = 1000 - 149;
			break;
			
			case BolacuaSite.COMO_LLEGAR:
			nHeight = 1290 - 149;
			break;
			
			case BolacuaSite.TARIFAS:
			nHeight = 1640 - 149;
			break;
			
			case BolacuaSite.EVENTOS:
			nHeight = 1820 - 149;
			break;
			
			case BolacuaSite.CONTACTO:
			nHeight = 550 - 149;
			break;
		}
		mcMiddle._height = nHeight;
		nHeight += mcBottom._height;
		mcMask.mcMiddle._height = mcMiddle._height - mcMask.mcBottom._height + 2;
		mcMask.mcBottom._y = mcMask.mcMiddle._y + mcMask.mcMiddle._height - 2;
		this.fadeIn( { bind: this, listener: showPhoto, ratio: 5 });
	}
	
	private function showPhoto():Void {
		
		//BolacuaSite.getApp().log("PhotoPopUp showPhoto= " + data.photos[ data.index ].getPromo());
		
		if ( data.photos[ data.index ].getPromo() ) {
			BolacuaSite.getApp().addEventListener( BolacuaSite.SECTION_CHANGED, Delegate.create(this, hide));
			mcPic = this.attachMovie("photoPopUpPic_promo", "mcPic", mcPic.getDepth());
			
		} else if ( data.photos[ data.index ].getHorizontal() ) {
			mcPic = this.attachMovie("photoPopUpPic_horizontal", "mcPic", mcPic.getDepth());
			
		} else {
			mcPic = this.attachMovie("photoPopUpPic_vertical", "mcPic", mcPic.getDepth());
		}
		if ( data.index == 0) mcPic.mcPrev._visible = false;
		if ( data.index == data.photos.length - 1) mcPic.mcNext._visible = false;
		mcPic._alpha = 0;
		mcPic._x = mcMiddle._x + (mcMiddle._width - mcPic._width) / 2;
		mcPic._y = data.posY - mcPic._height / 2;
		if( _root.bBrowser ){
			getURL("javascript:scrollWindowTo("+Math.round(this._y + mcPic._y)+");");
		}
		mcPic.addEventListener( PhotoPopUpPic.NEXT_PHOTO, Delegate.create(this, this.nextPhoto) );
		mcPic.addEventListener( PhotoPopUpPic.PREV_PHOTO, Delegate.create(this, this.prevPhoto) );
		mcPic.addEventListener( PhotoPopUpPic.CLOSE, Delegate.create(this, this.hide) );
		mcPic.fadeIn( { bind: this, listener: picShowEnd, ratio: 5 } );
	}
	
	private function picShowEnd():Void {
		mcPic.setData(data.photos[ data.index ]);
	}
	
	public function nextPhoto() {
		data.index ++;
		if (data.index == data.photos.length) data.index = 0;
		showPhoto();
	}
	public function prevPhoto() {
		data.index --;
		if (data.index < 0) data.index = data.photos.length - 1;
		showPhoto();
	}
	
	public function hide() {
		mcPic.fadeOut( { bind: this, listener: picHideEnd, ratio: 5 } );
	}
	
	private function picHideEnd():Void {
		mcPic = this.createEmptyMovieClip("mcPic", mcPic.getDepth());
		this._visible = false;
		this.bShowed = false;
	}
	
}